package com.ruoyi.zsyh.controller.applet.agent;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.PingYinUtil;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;

/**
 * 会长店铺产品Controller
 * 
 * @author yuexl
 * @date 2021-12-08
 */
@Controller
@RequestMapping("/applet/zsyh/agentParts")
public class ZsyhAgentPartsAppletController extends BaseController
{


    @Autowired
    private IZsyhPlatformPartstypeService zsyhPlatformPartstypeService;

    @Autowired
    private IZsyhAgentPartsService zsyhAgentPartsService;

    @Autowired
    private IZsyhAgentPartsattributeService zsyhAgentPartsattributeService;


    /**
     * 获取我的产品列表
     * @param isShow 是否上架（1上架0下架）
     * @return
     */
    @RequestMapping("/getAgentPartsList")
    @ResponseBody
    public AjaxResult getAgentPartsList(String agentId, String isShow){
        if(EmptyUtils.isEmpty(agentId)){
            return AjaxResult.error("暂未登录");
        }
        ZsyhAgentParts parts = new ZsyhAgentParts();
        parts.setIsShow(isShow);
        parts.setAgentId(agentId);
        return AjaxResult.success("获取成功", zsyhAgentPartsService.selectZsyhAgentPartsList(parts));
    }

    /**
     * 获取Id
     * @return
     */
    @RequestMapping("/obtainId")
    @ResponseBody
    public AjaxResult obtainId(){
        return AjaxResult.success("获取成功", IdWorker.getId());
    }

    /**
     * 保存产品型号
     * @return
     */
    @RepeatSubmit
    @RequestMapping("/savePartsAttribute")
    @ResponseBody
    @Transactional
    public AjaxResult savePartsAttribute(String partsId, String partsJson){
        //解析产品json数据
        List<Map<String, Object>> list = JSON.parseObject(partsJson, new TypeReference<List<Map<String, Object>>>() {});
        List<ZsyhAgentPartsattribute> arrList = new ArrayList<>();
        String partsIdNew = IdWorker.getId();
        Date date = new Date();
        long i = 1L;
        for(Map<String, Object> a:list){
            String attributeId = String.valueOf(a.get("id"));
            String attributeName = String.valueOf(a.get("attributeName"));
            String attributeLogo = String.valueOf(a.get("attributeLogo"));
            String attributeSalePrice = String.valueOf(a.get("attributeSalePrice"));
            String attributeStock = String.valueOf(a.get("attributeStock"));
            String attributeRemarks = String.valueOf(a.get("attributeRemarks"));
            String isPost = String.valueOf(a.get("isPost"));


            ZsyhAgentPartsattribute attribute = ZsyhAgentPartsattribute.getInstance();
            if(EmptyUtils.isEmpty(attributeId)){
                attributeId = IdWorker.getId();
            }
            attribute.setId(attributeId);
            attribute.setCreateTime(date);
            attribute.setAttributeName(attributeName);
            attribute.setAttributeSort(i);
            attribute.setAttributeSalePrice(new BigDecimal(attributeSalePrice));
            attribute.setPartsId(partsIdNew);
            attribute.setAttributeRemarks(attributeRemarks);
            attribute.setAttributeStock(attributeStock);
            attribute.setAttributeLogo(attributeLogo);
            attribute.setIsPost(isPost);
            attribute.setIsShow("1");
            attribute.setPartsId(partsId);
            arrList.add(attribute);
            i++;
        }
        zsyhAgentPartsService.batchSaveZsyhAgentPartsattribute(arrList, partsId);
        return success("操作成功", arrList);


    }

    /**
     * 新增产品
     * @return
     */
    @RepeatSubmit
    @RequestMapping("/saveMyParts")
    @ResponseBody
    @Transactional
    public AjaxResult saveMyParts(String partsId, String agentId, String partsName, String partsType, String partsUnit,
                                  String isHotPush, String isPost, String partsDetails, String partsImages, String isShow){

        ZsyhAgentParts parts = new ZsyhAgentParts();//赋值产品信息给会长产品
        parts.setId(partsId);
        parts.setAgentId(agentId);
        parts.setPartsName(partsName);
        parts.setPartsType(partsType);
        parts.setPartsDetails(partsDetails);
        parts.setPartsImages(partsImages);
        parts.setIsHotPush(isHotPush);
        parts.setIsPost(isPost);
        parts.setIsShow(isShow);
        parts.setPartsDistinguish("2");
        parts.setPartsUnit(partsUnit);
        parts.setPartsId(null);

        StringBuffer sb = new StringBuffer();
        //编码规则，公司名称缩写+“_”+产品名字缩写+"_"+该产品在此分类下面的顺序
        String tempStr = PingYinUtil.getFirstSpell(partsName);
        sb.append(tempStr);
        sb.append("_");
        //查询该分类下面所有的产品
        ZsyhAgentParts zsyhParts1 = new ZsyhAgentParts();
        zsyhParts1.setPartsType(partsType);
        List<ZsyhAgentParts> zsyhPartsList = zsyhAgentPartsService.selectZsyhAgentPartsList(zsyhParts1);
        if(zsyhPartsList.size()>0){
            sb.append("00"+(zsyhPartsList.size()+1));
        }else{
            sb.append("001");
        }
        parts.setPartsNumber(sb.toString());

        ZsyhAgentParts zsyhParts= zsyhAgentPartsService.getAgentPartsMaxSort(agentId);
        if(zsyhParts != null){
            parts.setPartsSort(zsyhParts.getPartsSort() + 1);
        }else{
            parts.setPartsSort(1L);
        }
        zsyhAgentPartsService.insertZsyhAgentPartsApplet(parts);//保存
        return success("操作成功");


    }

    /**
     * 产品上架，下架
     * @param isShow 是否上架（1上架0下架）
     * @return
     */
    @RequestMapping("/updatePartsIsShow")
    @ResponseBody
    @Transactional
    public AjaxResult updatePartsIsShow(String id, String isShow){
        zsyhAgentPartsService.updateIsShow(isShow, id);//产品上下架，所有规格批量上下架
        return success("操作成功");
    }


    /**
     * 规格上架， 下架
     * @param isShow 是否上架（1上架0下架）
     * @return
     */
    @RequestMapping("/updateAttributeIsShow")
    @ResponseBody
    @Transactional
    public AjaxResult updateAttributeIsShow(String id, String isShow){
        ZsyhAgentPartsattribute attribute = new ZsyhAgentPartsattribute();
        attribute.setId(id);
        attribute.setIsShow(isShow);
        zsyhAgentPartsattributeService.updateZsyhAgentPartsattribute(attribute);
        return success("操作成功");
    }

    /**
     * 查看型号
     * @return
     */
    @RequestMapping("/getAttributeListByPartsId")
    @ResponseBody
    @Transactional
    public AjaxResult getAttributeListByPartsId(String id){
        ZsyhAgentPartsattribute attribute = new ZsyhAgentPartsattribute();
        attribute.setPartsId(id);
        return success("操作成功", zsyhAgentPartsattributeService.selectZsyhAgentPartsattributeList(attribute));
    }

    /**
     * 修改规格信息
     * @return
     */
    @RequestMapping("/updateAttribute")
    @ResponseBody
    @Transactional
    public AjaxResult updateAttribute(String id, String attributeLogo, String attributeName, String attributeSalePrice, String attributeStock, String attributeRemarks, String isPost){
        ZsyhAgentPartsattribute attribute = new ZsyhAgentPartsattribute();
        attribute.setId(id);
        attribute.setAttributeLogo(attributeLogo);
        attribute.setAttributeName(attributeName);
        attribute.setAttributeSalePrice(new BigDecimal(attributeSalePrice));
        attribute.setAttributeStock(attributeStock);
        attribute.setAttributeRemarks(attributeRemarks);
        attribute.setIsPost(isPost);
        zsyhAgentPartsattributeService.updateZsyhAgentPartsattribute(attribute);
        return success("操作成功");
    }


    /**
     * 查询产品信息
     * @return
     */
    @RequestMapping("/getPartsById")
    @ResponseBody
    @Transactional
    public AjaxResult getPartsById(String id){
        return success("操作成功", zsyhAgentPartsService.selectZsyhAgentPartsById(id));
    }


    /**
     * 修改产品
     * @return
     */
    @RequestMapping("/updateMyParts")
    @ResponseBody
    @Transactional
    public AjaxResult updateMyParts(String partsId, String partsName, String partsType,
                                    String partsUnit, String isHotPush, String isPost,
                                    String partsDetails, String partsImages){
        ZsyhAgentParts parts = new ZsyhAgentParts();//赋值产品信息给会长产品
        parts.setId(partsId);
        parts.setPartsName(partsName);
        parts.setPartsType(partsType);
        parts.setPartsDetails(partsDetails);
        parts.setPartsImages(partsImages);
        parts.setIsHotPush(isHotPush);
        parts.setIsPost(isPost);
        parts.setIsShow("1");
        parts.setPartsDistinguish("2");
        parts.setPartsUnit(partsUnit);
        parts.setPartsId(null);

        zsyhAgentPartsService.updateZsyhAgentPartsApplet(parts);//保存
        return success("操作成功");


    }
}
